package com.tanzc.mp3player;

import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;
import com.tanzc.mp3player.utils.ProcessUtil;
import javazoom.jl.decoder.JavaLayerException;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.FileSystemException;

/**
 * <p> description: description </p>
 *
 * @author tanzc
 * @date 15:55 2021/5/27
 **/
public class PlayerManagerV2 {
    private ProcessUtil processUtil;
    private Mp3PlayerV2 mp3Player;
    private Robot robot;
    private HotkeyListener hotkeyListener;

    Thread thread;


    public static void main(String[] args) {
        // 做一个jdk版本判断
        String[] javaVersion = System.getProperty("java.version").split("_");
        if(Integer.parseInt(javaVersion[0].split("\\.")[1]) > 8 || (Integer.parseInt(javaVersion[0].split("\\.")[1]) == 8 && Integer.parseInt(javaVersion[1]) > 144)) {
            System.err.println("jdk版本过高，无法进行全局热键监听，请使用jdk1.8.0_144版本。");
            System.exit(-1);
        }

        String mp3Dir = "G:\\music\\mp3";
        if(args.length > 0) {
            mp3Dir = args[0];
        }

        PlayerManagerV2 manager;
        try {
            manager = new PlayerManagerV2(new File(mp3Dir));
            manager.addKey();
            manager.addKeyEvent();

            manager.thread.start();
        } catch (FileSystemException | FileNotFoundException | AWTException e) {
            e.printStackTrace();
        }


    }

    public PlayerManagerV2(File mp3Dir) throws FileSystemException, FileNotFoundException, AWTException {
        processUtil = new ProcessUtil();
        mp3Player = new Mp3PlayerV2(mp3Dir, "mp3");
        robot = new Robot();
        hotkeyListener = new HotkeyListener() {
            @Override
            public void onHotKey(int code) {
                // 先清除本程序占用的热键
                clearKey();
                // 因为设置的热键都要ctl + shift，所以直接提到switch外部
                robot.keyPress(KeyEvent.VK_CONTROL);
                robot.keyPress(KeyEvent.VK_SHIFT);
                robot.delay(10);
                switch (code) {
                    case 1: {
                        // 想了下，这个ctl + shift + →好像用的也不多，这里就不模拟了，直接全局占用
//                        robot.keyPress(KeyEvent.VK_RIGHT);
//                        robot.delay(10);
//                        robot.keyRelease(KeyEvent.VK_RIGHT);
                        System.out.println("ctl + shift + →");
                        mp3Player.setControlKey(1);
                        break;
                    }
                    case 2: {
                        robot.keyPress(KeyEvent.VK_Q);
                        robot.delay(10);
                        // 退出播放后，释放ctl + shift键
                        robot.keyRelease(KeyEvent.VK_CONTROL);
                        robot.keyRelease(KeyEvent.VK_SHIFT);
                        robot.keyRelease(KeyEvent.VK_Q);
                        System.out.println("ctl + shift + q");
                        mp3Player.setControlKey(2);
                        System.exit(0);
                    }
                    default: {
                        break;
                    }
                }
                // 模拟敲下原热键，并执行了本程序逻辑，再次注册热键
                addKey();
            }
        };
        thread = new PlayThread(this);
    }

    public void addKey() {
        // ctl + shift + →
        JIntellitype.getInstance().registerHotKey(1, JIntellitype.MOD_CONTROL + JIntellitype.MOD_SHIFT, 39);
        // ctl + shift + q
        JIntellitype.getInstance().registerHotKey(2, JIntellitype.MOD_CONTROL + JIntellitype.MOD_SHIFT, 81);
    }

    private void clearKey() {
        JIntellitype.getInstance().unregisterHotKey(1);
        JIntellitype.getInstance().unregisterHotKey(2);
    }

    public void addKeyEvent() {
        JIntellitype.getInstance().addHotKeyListener(hotkeyListener);
    }

    static class PlayThread extends Thread {
        private PlayerManagerV2 manager;

        public PlayThread(PlayerManagerV2 manager) {
            this.manager = manager;
        }
        @Override
        public void run() {
            try {
                manager.mp3Player.playRotate();
            } catch (FileSystemException | FileNotFoundException | JavaLayerException e) {
                e.printStackTrace();
            }
        }
    }


}
